package com.it.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.constants.SystemConstants;
import com.it.domain.ResponseResult;
import com.it.domain.dto.LinkDTO;
import com.it.domain.entity.Link;
import com.it.domain.entity.User;
import com.it.domain.vo.LinkVo;
import com.it.enums.AppHttpCodeEnum;
import com.it.mapper.LinkMapper;
import com.it.mapper.UserMapper;
import com.it.service.LinkService;
import com.it.service.UserService;
import com.it.utils.BeanCopyUtils;
import com.it.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2024-08-04 16:49:35
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.Link_STAUS_NORMAL);
        List<Link> links = list(queryWrapper);
        for(Link link : links){
            User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId,link.getUserId()));
            link.setAvatar(user.getAvatar());
        }
        //封装成vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult applyLink(LinkDTO linkDTO) {
        Link link = BeanCopyUtils.copyBean(linkDTO, Link.class);
        link.setUserId(SecurityUtils.getUserId());
        if (this.save(link)){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.LINK_ERROR);
    }
}

